package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_HUA_SHI_GANG_UPDATE_AUTO_EVENT_SWITCH, desc = "修改自动处理开关")
public class UpdateAutoEventSwitchCmd extends AbstractCommand {

    private List<Integer> openSwitchList = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.UpdateAutoEventSwitchReqMsg reqMsg = HuaShiGangProto.UpdateAutoEventSwitchReqMsg.parseFrom(packet.getBytes());
        openSwitchList = reqMsg.getOpenSwitchTypeList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 活动期间内才能操作
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }

        if (!HuaShiGangMgr.inOperateTime(activityInfo)) {
            ret = GameErrorCode.E_HUASHIGANG_NOT_OPERATE_TIME;
            return;
        }

        HuaShiGangProto.CrossUpdateAutoEventSwitchReqMsg.Builder builder = HuaShiGangProto.CrossUpdateAutoEventSwitchReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.addAllOpenSwitchType(openSwitchList);
        builder.setServerId(player.getUserInfo().getOriginalServerId());
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_UPDATE_AUTO_EVENT_SWITCH, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.UpdateAutoEventSwitchRespMsg.Builder builder = HuaShiGangProto.UpdateAutoEventSwitchRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_UPDATE_AUTO_EVENT_SWITCH, builder);
        }
    }
}
